package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.enums.VideoType;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.mongo.VideoComment;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoCommentApiImpl implements VideoCommentApi{
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Boolean hasVideo(String videoId, Long userId, VideoType like) {
        Criteria criteria = Criteria.where("videoId").is(new ObjectId(videoId))
                .and("userId").is(userId).and("videoType").is(like.getType());
        Query query =Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, VideoComment.class);
        return exists;
    }


    //视频点赞功能
    @Override
    public Integer save(VideoComment videoComment) {
        //查询视频
        Video vide = mongoTemplate.findById(videoComment.getVideoId(), Video.class);
        if (vide!=null){
            //向VideoComment对象中设置被评论人属性
            videoComment.setPublishUserId(vide.getUserId());
        }
        //保存到数据库中
        mongoTemplate.save(videoComment);
        //更行视频表中对应得字段
        Query query =Query.query(Criteria.where("id").is(videoComment.getVideoId()));
        Update update = new Update();
        if (videoComment.getVideoType()==VideoType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if (videoComment.getVideoType()==VideoType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置更行参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        //获取最新得评论数量并且返回
        Video andModify = mongoTemplate.findAndModify(query, update, options, Video.class);
        Integer integer = andModify.statisCount(videoComment.getVideoType());
        return integer;
    }

    @Override
    public Integer delete(VideoComment videoComment) {
        //删除互动表中得数据
        Criteria criteria = Criteria.where("videoId").is(videoComment.getVideoId())
                .and("userId").is(videoComment.getUserId())
                .and("videoType").is(videoComment.getVideoType());
        Query query =Query.query(criteria);
        mongoTemplate.remove(query,VideoComment.class);
        //更新视频表中得数据
        Query query1 =Query.query(Criteria.where("id").is(videoComment.getVideoId()));
        Update update = new Update();
        if (videoComment.getVideoType()==VideoType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if (videoComment.getVideoType()==VideoType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更行参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        //获取最新得评论数量并且返回
        Video andModify = mongoTemplate.findAndModify(query1, update, options, Video.class);
        Integer integer = andModify.statisCount(videoComment.getVideoType());
        return integer;
    }


    //查询评论
    @Override
    public List<VideoComment> findComments(String id, Integer page, Integer pagesize, int type) {
        Criteria criteria = Criteria.where("videoId").is(new ObjectId(id)).and("videoType").is(type);
        Query query = Query.query(criteria)
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<VideoComment> list = mongoTemplate.find(query, VideoComment.class);
        return list;
    }


    //评论点赞
    @Override
    public VideoComment findByIdComment(String id, Long userId) {
        Criteria criteria = Criteria.where("id").is(id).and("videoType").is(2);
        Query query = Query.query(criteria);
        VideoComment videoComment = mongoTemplate.findOne(query, VideoComment.class);
        List<Long> commentLike = videoComment.getCommentLike();
        if (CollUtil.isEmpty(commentLike)){
            commentLike.add(userId);
        }else {
            for (Long aLong : commentLike) {
                if (aLong != userId){
                    commentLike.add(userId);
                }
            }
        }
        Update update = new Update();
        update.set("commentLike",commentLike);
        update.inc("likeCount",1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(false) ;//不获取更新后的最新数据
        VideoComment andModify = mongoTemplate.findAndModify(query, update, options, VideoComment.class);
        andModify.setLikeCount(videoComment.getLikeCount()+1);
        return andModify;
    }

    @Override
    public VideoComment findByIdComment1(String id, Long userId) {
        Criteria criteria = Criteria.where("id").is(id).and("videoType").is(2);
        Query query = Query.query(criteria);
        VideoComment videoComment = mongoTemplate.findOne(query, VideoComment.class);
        List<Long> commentLike = videoComment.getCommentLike();
        boolean remove = commentLike.remove(userId);
        Update update = new Update();
        update.set("commentLike",commentLike);
        update.inc("likeCount",-1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(false) ;//不获取更新后的最新数据
        VideoComment andModify = mongoTemplate.findAndModify(query, update, options, VideoComment.class);
        andModify.setLikeCount(videoComment.getLikeCount()-1);
        return andModify;
    }
}
